package cn.antia.cep.page.impl;

import cn.antia.cep.common.AntiaLog;
import cn.antia.cep.db.hibernate.bean.Resource;
import cn.antia.cep.page.TreePrinter;
import cn.antia.cep.right.RightTree;

import javax.servlet.jsp.JspWriter;
import java.io.IOException;
import java.util.List;

/**
 * 使用Mxtree
 * User: xiao
 * Date: 2007-1-31
 * Time: 16:00:48
 */
public class MztreeTreePrinter implements TreePrinter {
    /**
     * 递归打印权限树(打印成HTML)
     *
     * @param out    jspwriter
     * @param rightTree 权限树
     */
    public void recursivePrint(RightTree rightTree, JspWriter out,String operate) {
        try{
			out.println("<div id=treeviewarea style=\"background-color:#FFFEEE\"></div>");
			out.println("<SCRIPT LANGUAGE=\"JavaScript\">");
			out.println("var tree = new MzTreeView(\"tree\");");
			//out.println("tree.setIconPath(\"./\");");
			out.println("tree.setIconPath('resource/img/');");
			//递归输出
			recursiveMztree(rightTree,out,operate);
			out.println("document.getElementById('treeviewarea').innerHTML = tree.toString();");
			out.println("</SCRIPT>");
        }catch(IOException ioe){
            AntiaLog.error(new StringBuffer().append("打印权限树时出现IO异常：").append(ioe.getLocalizedMessage()).toString());
            ioe.printStackTrace();
        }
    }

    /**
	 * 递归输出树上的节点
     * (使用Mztree控件)
	 *
     * @param antiaTree AntiaTree实例
     * @throws java.io.IOException io
     * @param out 打印输出对象*/
	private void recursiveMztree(RightTree antiaTree,JspWriter out,String operate) throws IOException {
		if(antiaTree.hasChild()){
			Resource node = (Resource)antiaTree.getNode();
			printNodeMz(node,out,operate);
			List cs = antiaTree.getChild();
            for (java.util.Iterator it = cs.iterator(); it.hasNext();) {
                Object c = it.next();
                RightTree tree = (RightTree) c;
                recursiveMztree(tree, out, operate);
            }
        }else{
        	Resource node = (Resource)antiaTree.getNode();
			printNodeMz(node,out,operate);
        }
	}

	/**
	 * 打印一个节点
	 * (使用Mztree控件)
     * @param node 要打印的结点
     * @throws java.io.IOException io
     * @param out 打印对象*/
	private void printNodeMz(Resource node,JspWriter out,String operate) throws IOException{
		StringBuffer nodeString = new StringBuffer("tree.nodes['");
		if(node.getParentId() == null){
			node.setParentId(new Integer(-1));
		}
		nodeString.append(node.getParentId()).append("_").append(node.getId()).append("']").append(" = '");
		nodeString.append("text:").append(node.getName());
		//如果是管理操作，则链接到自身的URL。否则链接到需要链接的URL
		if(operate.equalsIgnoreCase("mana")){
			nodeString.append(";url:").append(node.getUrlSelf());
		}else if(operate.equalsIgnoreCase("disp")){
			nodeString.append(";url:").append(node.getUrlLink());
		}
		nodeString.append(";target:").append(node.getTarget());
		//如果是管理操作，则在客户端执行MANAGER方法，否则，执行DISPLAY方法
		if(operate.equalsIgnoreCase("disp")){
			nodeString.append(";method:").append(node.getMethodDisp());
		}else if(operate.equalsIgnoreCase("mana")){
			nodeString.append(";method:").append(node.getMethodMana());
		}
		//如果是管理操作，则取出链接到自身的参数，否则，取出链接到链接目标的参数
		if(operate.equalsIgnoreCase("mana") && node.getParaSelf() != null){
			nodeString.append(";data:").append(node.getParaSelf());
		}else if(operate.equalsIgnoreCase("disp") && node.getParaLink() != null){
			nodeString.append(";data:").append(node.getParaLink());
		}
		if(node.getTitle() != null){
			nodeString.append(";hint:").append(node.getTitle());
		}
		nodeString.append("';");
		out.println(nodeString.toString());
    }
}
